import Vue from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import store from './store'



import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)


axios.interceptors.request.use(config=>{
    console.log(config);
    let token = localStorage.getItem("token") || '';
    config.headers['token'] = token;
    return config;
},err=>{
    return Promise.reject(err);
})

//http response 拦截器
axios.interceptors.response.use(response=>{
    console.log("responseData",response.data.data);
    return response;
},err=>{
    return Promise.reject(err.response.status);
});



Vue.config.productionTip = false

//路由之前代码都会走到这个地方，我可以修改路由
router.beforeEach((to,from,next)=>{
    console.log(to,from,next);
    if(to.path=='/index'){
        let childrenRouters = [];
        console.log(router.options);
        axios.get("http://localhost:8888/menu/getTreeData").then(res=>{
            let  menuTreeData =  res.data.data;
            // debugger
            let childrenRouters = [];
            console.log(menuTreeData)
            menuTreeData.forEach(menu =>{
                menu.subMenuList.forEach(subMenu=>{
                    let newRouter = {
                        name: subMenu.name,
                        path: subMenu.path
                    }
                    newRouter.component = () => import( './views/'+subMenu.component+'.vue');
                    childrenRouters.push(newRouter);
                })
            })
            console.log(childrenRouters)
            router.options.routes[0].children = childrenRouters;
            router.addRoutes(router.options.routes);
        })
    }
    next();
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
